﻿/// <reference path="../../KennyPublic.js" />
/// <reference path="../../gird.public.js" />
; (function () {
    $(function () {
        page.init();
    });
    var _bt = window.BT;
    var page = {
        init: function () {
            this.event.init();
        }
        , dialog: _bt.dialog
        , ajax: _bt.ajax
        , event: {}
        , grid: {}
        , config: {
            grid: {
                id: '#grid'
                 ,selection: true
                 ,rowSelect: true
                 , url: '/PatentMonitor/GetYearFeeMonitorData'
                 , formatters: {
                     "link": function (column, row) {
                         return '<div class="ui-pg-div" >'
                                    + '<a href="#" class="ui-icon icon-ok blue" title="快捷添加监控" data-row-id="' + row['Id'] + '"></a>'
                                    + '<a href="#" class="ui-icon icon-remove blue" title="快捷取消监控" data-row-id="' + row['Id'] + '"></a>'
                                + '</div>';
                     }
                 }
            }
        }
    };
    page.grid = new _bt.BGrid(page.config.grid);
    page.event.init = function () {
        $('#btn-search').on('click', search);
        //添加监控
        $(page.config.grid.id + ' tbody').on('click', '.icon-ok', function () {
            singleChangeMonitor.call(this, true);
        });
        //取消监控
        $(page.config.grid.id + ' tbody').on('click', '.icon-remove', function () {
            singleChangeMonitor.call(this, false);
        });

        //添加监控
        $('#btn-add-monitor').on('click', function () {
            bulkChangeMonitor(true);
        });
        //取消监控
        $('#btn-remove-monitor').on('click', function () {
            bulkChangeMonitor(false);
        });
    }

    function search() {
        var obj = $('#search-form').serializeObject();
        page.grid.reload(obj);
    }
    //单个监控修改
    function singleChangeMonitor(state) {
        var self = $(this);
        var rowId = self.data('rowId');
        changeMonitor({ YearFeeIsRemind: state, Id: rowId }, false);
        return false;
    }
    //多个监控修改
    function bulkChangeMonitor(state) {
        var rows = getSelection();
        if (!rows.isSuccess) {
            return;
        }
        changeMonitor({ YearFeeIsRemind: state, Ids: rows.rows.join(',') }, true);
    }
    //修改监控状态
    function changeMonitor(data, isBulk) {
        var url = isBulk === true ? '/PatentMonitor/BulkModifyYearFeeRemind' : '/PatentMonitor/ModifyYearFeeRemind';
        var tip = data.YearFeeIsRemind === true ? '添加监控成功' : '取消监控成功';
        var success = function (d) {
            if (d && d.isSuccess) {
                page.dialog.info(tip);
                search();//刷新
            } else {
                page.dialog.warn(d.msg || '操作失败');
            }
        }
        page.ajax({ url: url, data: data }, success);
    }
    //获取选中行
    function getSelection() {
        var result = { isSuccess: true };
        var selection = page.grid.getSelections();
        if (selection.length == 0) {
            page.dialog.warn('请选择一条数据');
            result.isSuccess = false;
            return result;
        }
        result['rows'] = selection;
        return result;
    }

})();